package com.heima.test.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.junit.Test;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

public class ConsumerQuickStart {

    //消费者

    @Test
    public void onMsg(){

        //2.创建对象完成配置
        Properties properties = new Properties();
        //连接地址
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"39.107.100.177:9092");
        //消费者组
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"java58");
        //消息反序列化器
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");

        //1.创建消费者核心对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        //3.订阅一个或多个topic主题
        consumer.subscribe(Collections.singletonList("order"));

        //4.拉取消息进行消费,消费者是要一直处于监听接收状态
        while (true) {
            //获取消息
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));

            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key()+"--------"+record.value());
            }
        }

    }

    @Test
    public void onMsgAsync(){
        //2.创建对象完成配置
        Properties properties = new Properties();
        //连接地址
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"39.107.100.177:9092");
        //消费者组
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"java58");
        //消息反序列化器
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");

        //1.创建消费者核心对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        //3.订阅一个或多个topic主题
        consumer.subscribe(Collections.singletonList("order"));

        //4.拉取消息进行消费,消费者是要一直处于监听接收状态
        //同步异步提交
        try {
            while (true) {
                //获取消息
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));

                for (ConsumerRecord<String, String> record : records) {
                    System.out.println(record.key()+"--------"+record.value());
                }
                //正常的就是异步提交,一旦出现异常就进行同步提交知道成功
                consumer.commitAsync();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //同步提交
                consumer.commitSync();
            } finally {
                consumer.close();
            }
        }
    }
}
